package com.ruoyi.weixin.shop.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.weixin.shop.domain.YxStoreProduct;
import com.ruoyi.weixin.shop.dto.YxStoreProductQueryCriteria;
import com.ruoyi.weixin.shop.service.YxStoreProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @author hupeng
 * @date 2019-10-04
 */
@Api(tags = "商城:商品管理")
@RestController
@RequestMapping("api")
public class StoreProductController {

    private final YxStoreProductService yxStoreProductService;

    public StoreProductController(YxStoreProductService yxStoreProductService) {
        this.yxStoreProductService = yxStoreProductService;
    }

    @ApiOperation(value = "查询商品")
    @GetMapping(value = "/yxStoreProduct")
	@PreAuthorize("@ss.hasAnyPermi({'shop:product:list','shop:goods:list'})")
    public ResponseEntity getYxStoreProducts(YxStoreProductQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(yxStoreProductService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation(value = "新增商品")
    @PostMapping(value = "/yxStoreProduct")
	@PreAuthorize("@ss.hasAnyPermi({'shop:product:add','shop:goods:add'})")
    public ResponseEntity create(@Validated @RequestBody YxStoreProduct resources) {
        if (ObjectUtil.isEmpty(resources.getGiveIntegral())) {
            resources.setGiveIntegral(BigDecimal.ZERO);
        }
        if (ObjectUtil.isEmpty(resources.getCost())) {
            resources.setCost(BigDecimal.ZERO);
        }
        return new ResponseEntity(yxStoreProductService.saveProduct(resources), HttpStatus.CREATED);
    }

    @ApiOperation(value = "修改商品")
    @PutMapping(value = "/yxStoreProduct")
	@PreAuthorize("@ss.hasAnyPermi({'shop:product:edit','shop:goods:edit'})")
    public ResponseEntity update(@Validated @RequestBody YxStoreProduct resources) {
        yxStoreProductService.updateProduct(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ApiOperation(value = "删除商品")
    @DeleteMapping(value = "/yxStoreProduct/{id}")
	@PreAuthorize("@ss.hasAnyPermi({'shop:product:delete','shop:goods:delete'})")
    public ResponseEntity delete(@PathVariable Integer id) {
        yxStoreProductService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "恢复数据")
    @DeleteMapping(value = "/yxStoreProduct/recovery/{id}")
	@PreAuthorize("@ss.hasAnyPermi({'shop:product:recovery','shop:goods:recovery'})")
    public ResponseEntity recovery(@PathVariable Integer id) {
        yxStoreProductService.recovery(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "商品上架/下架")
    @PostMapping(value = "/yxStoreProduct/onsale/{id}")
    public ResponseEntity onSale(@PathVariable Integer id, @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        int status = Integer.valueOf(jsonObject.get("status").toString());
        yxStoreProductService.onSale(id, status);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "生成属性")
    @PostMapping(value = "/yxStoreProduct/isFormatAttr/{id}")
    public ResponseEntity isFormatAttr(@PathVariable Integer id, @RequestBody String jsonStr) {
        return new ResponseEntity(yxStoreProductService.isFormatAttr(id, jsonStr), HttpStatus.OK);
    }

    @ApiOperation(value = "设置保存属性")
    @PostMapping(value = "/yxStoreProduct/setAttr/{id}")
    public ResponseEntity setAttr(@PathVariable Long id, @RequestBody String jsonStr) {
        yxStoreProductService.createProductAttr(id, jsonStr);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "清除属性")
    @PostMapping(value = "/yxStoreProduct/clearAttr/{id}")
    public ResponseEntity clearAttr(@PathVariable Long id) {
        yxStoreProductService.clearProductAttr(id, true);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "获取属性")
    @GetMapping(value = "/yxStoreProduct/attr/{id}")
    public ResponseEntity attr(@PathVariable Integer id) {
        String str = yxStoreProductService.getStoreProductAttrResult(id);
        if (StrUtil.isEmpty(str)) {
            return new ResponseEntity(HttpStatus.OK);
        }
        JSONObject jsonObject = JSON.parseObject(str.replaceAll("\\\\",""));
        return new ResponseEntity(jsonObject, HttpStatus.OK);
    }
}
